import { Component } from '@angular/core';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';
import { _HttpClient } from '@delon/theme';
import { EditComponent } from 'src/app/routes/common/edit/edit.component';
import { SimpleSql } from '@api/simple/model/simpleSql.model';
import { map } from 'rxjs/operators';
import { SFSchemaEnum } from '@delon/form';
import { SimpleSqlService } from '@api/simple/service/simpleSql.service';
import { SimpleDatasourceService } from '@api/simple/service/simpleDatasource.service';

@Component({
  selector: 'app-simple-sql-edit',
  templateUrl: './edit.component.html',
})
export class SimpleSqlEditComponent extends EditComponent<SimpleSql, String> {
  constructor(
    public modal: NzModalRef,
    public msgSrv: NzMessageService,
    public http: _HttpClient,
    public simpleSqlService: SimpleSqlService,
    public simpleDatasourceService: SimpleDatasourceService,
  ) {
    super(modal, msgSrv, http, simpleSqlService);
    this.schema = {
      properties: {
        code: { type: 'string', title: '代码' },
        name: { type: 'string', title: '名称', maxLength: 30 },
        datasourceId: { type: 'string', title: '数据源' },
        sql: { type: 'string', title: 'SQL' },
      },
      required: ['code', 'name', 'datasourceId'],
    };
    this.ui = {
      '*': {
        spanLabelFixed: 100,
        grid: { span: 12 },
      },
      $datasourceId: {
        widget: 'select',
        serverSearch: true,
        searchDebounceTime: 300,
        searchLoadingText: '搜索中...',
        onSearch: (name: any) => {
          return this.simpleDatasourceService
            .findByQueryParams({ name: name })
            .pipe(map((res) => (res.list as any[]).map((i) => ({ label: i.name, value: i.id } as SFSchemaEnum))))
            .toPromise();
        },
      },
      $sql: {
        widget: 'textarea',
        autosize: { minRows: 10, maxRows: 20 },
        grid: { span: 24 },
      },
    };
  }
}
